package com.ftar.urgent.http;


import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.ftar.urgent.http.constant.ConstHttpUrl;

import java.io.IOException;
import java.util.Calendar;
import java.util.HashMap;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;


/**
 * @author: wei.jin
 * @date: 2023/7/8
 * @email: wei.jin@fanttec.com
 * @description：
 */
public class HttpUtils {

    private static OkHttpClient client = new OkHttpClient();

    private static String URL_PREFIX = "https";


    public static String get(final String url) throws IOException {
        Request request = new Request.Builder()
                .url(url)
                .build();

        Response response = client.newCall(request).execute();
        return response.body() == null ? null : response.body().string();
    }

    public static void get(final String url,
                           final FtHttpResponseHandler responseHandler) {

        if (!url.substring(0, 4).equals(URL_PREFIX)) {
            return;
        }
        final Request request = new Request.Builder().url(url).build();
        client.newCall(request).enqueue(responseHandler);
    }

    public static String post(final String url, final RequestBody body) throws IOException {
        final Request request = new Request
                .Builder()
                .url(url)
                .post(body).build();
        Response response = client.newCall(request).execute();
        if (!response.isSuccessful()) {
            return null;
        }
        return response.body().string();
    }

    public static void post(final String url, String token, final RequestBody body,
                            final FtHttpResponseHandler responseHandler) {
        if (!url.startsWith(URL_PREFIX)) {
            return;
        }
        token = "Bearer " + token;
        final Request request = new Request
                .Builder()
                .url(url)
                .addHeader("Authorization", token)
                .post(body).build();
        client.newCall(request).enqueue(responseHandler);
    }

    public static void download(final String url, final FtHttpResponseHandler responseHandler) {
        if (!url.substring(0, 4).equals(URL_PREFIX)) {
            return;
        }
        final Request request = new Request.Builder().url(url).build();
        client.newCall(request).enqueue(responseHandler);
    }

    public static void upload(final String url, final RequestBody body, final FtHttpResponseHandler responseHandler) {
        if (!url.substring(0, 4).equals(URL_PREFIX)) {
            return;
        }
        final Request request = new Request.Builder().url(url).post(body).build();
        client.newCall(request).enqueue(responseHandler);
    }

    /**
     * Get token for unicom api request
     *
     * @return
     */
    public static String getUnicomToken() {
        // 指定token过期时间为20分钟
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.SECOND, 1200);

        HashMap headerMap = new HashMap();
        headerMap.put("alg", "HS256");
        headerMap.put("typ", "JWT");

        return JWT.create()
                .withHeader(headerMap)  // Header
                .withClaim("iss", ConstHttpUrl.UNICOM_API_KEY)  // Payload
                .withExpiresAt(calendar.getTime())  // 过期时间
                .sign(Algorithm.HMAC256(ConstHttpUrl.UNICOM_SECRET_KEY));  // 签名用的secret
    }
}
